<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/13
 * Time: 10:31
 */

namespace api\storage\controller;


use api\storage\common\Constant;
use api\storage\model\BomModel;
use api\storage\model\StaffStorageModel;
use api\storage\model\StorageModel;
use think\Cache;
use think\Request;

class StorageController extends AppBaseController
{
    private $storageModel;
    private $bomModel;

    public function __construct(Request $request = null, StorageModel $storageModel, BomModel $bomModel)
    {
        parent::__construct($request);
        $this->storageModel = $storageModel;
        $this->bomModel = $bomModel;
    }

    /**
     * @api {GET} /api/storage/storage 获取所有仓库
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  index 获取所有仓库
     * @apiGroup  storage
     * @apiVersion 0.0.1
     * @apiDescription  获取所有仓库
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 获取成功
     * @apiSuccess {Object} data
     * @apiSuccess {Array} data.storageList 仓库列表
     * @apiError  Error
     **/
    public function index()
    {
        $userId = $this->getUserId();
        $staffModel = new StaffStorageModel();
        $storageList = $staffModel->queryStorage($userId);
        $this->success('获取成功', ['storageList' => $storageList]);
    }

    /**
     * @api {PUT} /api/storage/storage/curr_storage_update 更新当前仓库
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  updateCurrStorage 更新当前仓库
     * @apiGroup  storage
     * @apiVersion 0.0.1
     * @apiDescription  更新当前仓库
     *
     * @apiParam {string} uuid 设置为当前仓库的仓库uuid
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 信息
     * @apiSuccess {Object} data
     * @apiError  Error
     **/
    public function updateCurrStorage()
    {
        $uuid = input('uuid');
        $userId = $this->getUserId();
        $staffModel = new StaffStorageModel();
        $currStorage = $staffModel->findCurrentStorage($userId);
        if (!empty($currStorage)) {
            $staffModel->updateNotCurrentStorage($userId, $currStorage->storage_uuid);
        }
        $res = $staffModel->updateCurrentStorage($userId, $uuid);
        if ($res) {
            //将之前的缓存去掉
            Cache::set(Constant::CACHE_PREFIX_USER . $userId, null);
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }

    /**
     * @api {GET} /api/storage/storage/curr_storage_query 查询当前仓库是否在出库
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  queryStorageOutput 查询当前仓库是否在出库
     * @apiGroup  storage
     * @apiVersion 0.0.1
     * @apiDescription  查询当前仓库是否在出库
     *
     * @apiParam {string} uuid 获取当前仓库的仓库uuid
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 信息
     * @apiSuccess {Object} data
     * @apiError  Error
     */
    public function queryStorageOutput(){
        //$uuid=input('uuid');
        $uuid = $this->getStorageUuid();
        $result=$this->storageModel->findStorageByUuid($uuid);
        $str=$result['bom_uuid'];
        if(!empty($str)){
            $where['uuid'] = $str;
            $bom = $this->bomModel->findBomInfo($where)->toArray();
            $this->success('正在出库的工单号为：',$bom['order_id']);
        }
        else{
            $this->error('无正在出库的工单');
        }

    }
    /**
     * @api {POST} /api/storage/storage/curr_storage_delete 清除当前仓库的bomUuid
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  delStorageBom 清除当前仓库的bomUuid
     * @apiGroup  storage
     * @apiVersion 0.0.1
     * @apiDescription  清除当前仓库的bomUuid
     *
     * @apiParam {string} uuid 获取当前仓库的仓库uuid
     * @apiParam {string} bomUuid 要修改的bomUuid的值
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 信息
     * @apiSuccess {Object} data
     * @apiError  Error
     */
    public function delStorageBom(){
       // $uuid=input('uuid');
        $uuid = $this->getStorageUuid();
        $bomUuid=null;
        $this->storageModel->updateBomByUuid($bomUuid,$uuid);
        $this->success('清除完成');
    }
}